#include <my_header.h>
typedef int DataType;

typedef struct node
{
    DataType date;
    struct node* next;
}Node;

typedef struct
{
    Node *head;
    Node *tail;
    int size;
}Linklist;

void * thread_func(void *arg);
Linklist *create_linked_list();
void destory_linked_list(Linklist *list);
bool insert_list(Linklist *list,DataType new_data);
void print_node(Linklist *list);
/* usage */
int main(int argc, char *argv[])
{
    ARGS_CHECK(argc,1);

    Linklist * list = create_linked_list();
    insert_list(list,10);
    insert_list(list,20);
    insert_list(list,30);
    insert_list(list,10);
    print_node(list);

    pthread_t tid;
    int ret = pthread_create(&tid,NULL,thread_func,list);
    ERROR_CHECK(ret,-1,"pthread_create");

    printf("I am main thread.\n");

    print_node(list);
    pthread_join(tid,NULL);
    destory_linked_list(list);

    return 0;
}

void * thread_func(void *arg)
{
    printf("I am child thread\n");
    Linklist * list = (Linklist *)arg;
    print_node(list);
    return NULL;
}


Linklist *create_linked_list()
{
    Linklist * list = (Linklist *)calloc(1,sizeof(Linklist));
    return list;
}


void destory_linked_list(Linklist *list)
{
    Node *cur = list->head;
    while(cur->next != NULL)
    {
        Node *tmp = cur->next;
        free(cur);
        cur = tmp;
    }
    free(list);

}


bool insert_list(Linklist *list,DataType new_data)
{
    Node * new_node = (Node *)malloc(sizeof(Node));
    if(new_node == NULL)
    {
        printf("malloc failed in insert_list.\n");
        return false;
    }

    new_node->date = new_data;
    new_node->next = list->head;

    list->head = new_node;

    if(list->tail == NULL)
    {
        list->tail = new_node;
    }
    list->size++;
    return true;
}

void print_node(Linklist *list)
{
    Node *cur = list->head;
    while(cur != NULL)
    {
        printf("%d",cur->date);
        if(cur->next != NULL)
        {
            printf("->");
        }
        cur = cur->next;
    }
    printf("\n");
}
